package cn.edu.zut.dao;

import cn.edu.zut.domin.Reader;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;


public interface IReaderDao {
    /**
     * 查询
     * @return
     * @throws Exception
     */
    @Select("select * from Reader")
    public List<Reader> findAll() throws Exception;

    /**
     * 查询读者ById
     * @return
     * @throws Exception
     */
    @Select("select * from Reader where readerId = #{id}")
    public List<Reader> findReaderById(String id) throws Exception;

    /**
     * 根据名字查询读者
     * @param userName
     * @return
     */
    @Select("select * from Reader where userName = #{userName}")
    public List<Reader> findReaderByUserName(String userName);

    @Select("select * from Reader where tel = #{tel}")
    public List<Reader> findByReaderTel(String tel);
    /**
     * 保存读者
     * @param
     * @throws Exception
     */
    @Insert("insert into Reader(readerId,userName,sex,job,company,tel,address,email,register)" +
            " values(#{readerId},#{userName},#{sex},#{job},#{company},#{tel},#{address},#{email},#{register})")
    public void saveReader(Reader reader) throws Exception;

    /**
     * 删除一个读者
     * @param id
     * @throws Exception
     */
    @Delete("delete from Reader where readerId=#{readerId}")
    public void deleteReader(String id) throws Exception;

    /**
     * 更新读者信息
     * @param reader
     * @throws Exception
     */
    @Update("update reader set readerId=#{readerId},userName=#{userName},sex=#{sex},job=#{job},company=#{company},tel=#{tel},address=#{address},email=#{email},register=#{register}" +
            " where readerId=#{readerId}")
    public void updateReader(Reader reader) throws Exception;

    /**
     * 根据读者ID来查找该读者以及该读者的借阅信息
     * @param readerId
     * @return
     */
    @Select("select * from Reader where readerId=#{readerId}")
    @Results({
            @Result(column="readerId",property="readerId"),
            @Result(property = "bookLendInfos", javaType = List.class, column = "readerId",
                    many = @Many(select = "cn.edu.zut.dao.IBookLendInfoDao.getBookLendInfoByReaderId",fetchType = FetchType.EAGER) )    })
    public List<Reader> selectBookLendInfoByReaderId(String readerId);

    /**
     * 查询所有读者及其借阅信息
     * @return
     */
    @Select("select * from reader")
    @Results({
            @Result(column="readerId",property="readerId"),
            @Result(property = "bookLendInfos", javaType = List.class, column = "readerId",
                    many = @Many(select = "cn.edu.zut.dao.IBookLendInfoDao.getBookLendInfoByReaderId",fetchType = FetchType.EAGER) )    })
    public List<Reader> selectAllBookLendInfoByReaderId();

    /**
     * 根据名字模糊查询(子串)
     * @param name
     * @return
     */
    @Select("select * from reader where username like  CONCAT('%',#{username},'%')")
    public List<Reader> findReaderByNameMOHU(String name);

    /**
     * 根据id查询读者
     * @return
     */
    @Select("select * from Reader where readerId=#{id}")
    public Reader findReaderById1(String id);
}
